package top.kaoshanji.ch05;

import com.google.common.io.Resources;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * author kaoshanji
 * ^_^
 * create time 2024/12/9 9:06
 */
public class DBUtil {

    private static final String FILE_NAME = "ch05/database.properties";

    /**
     * 获得数据库连接，故事开始的地方
     * @return Connection
     */
    public static Connection getConnection() throws IOException, SQLException {

        URL urlFile = Resources.getResource(FILE_NAME);
        var props = new Properties();
        props.load(urlFile.openStream());

        String url = props.getProperty("jdbc.url");
        String username = props.getProperty("jdbc.username");
        String password = props.getProperty("jdbc.password");

        return DriverManager.getConnection(url, username, password);

    }

}
